package cn.stronglink.assetmanage.business.inventory.model;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import cn.stronglink.assetmanage.MyApplication;
import cn.stronglink.assetmanage.R;
import cn.stronglink.assetmanage.dao.DaoManager;
import cn.stronglink.assetmanage.dao.InventoryDao;
import cn.stronglink.assetmanage.dao.InventoryDetailDao;
import cn.stronglink.assetmanage.entity.Inventory;
import cn.stronglink.assetmanage.entity.InventoryDetail;
import cn.stronglink.assetmanage.utils.Constants;

/**
 * Created by chencongcong on 2018/6/15.
 */

public class InventoryCountModel {

    //获得所有已盘点数据  暂定已盘点为非1
    public List<Inventory> getDataByOrderNoDistince() {
        List<Inventory> inventoryList = DaoManager.
                getDaoSession(MyApplication.APP_CONTEXT).getInventoryDao().queryBuilder().distinct()
                .orderDesc(InventoryDao.Properties.Id_).
                        where(InventoryDao.Properties.Status.notEq(Constants.STATUS_ONE)).list();
        DaoManager.getDaoSession(MyApplication.APP_CONTEXT).clear();
        if (inventoryList != null) {
            return inventoryList;
        }
        return null;
    }

    /**
     * 根据表单号获得二级列表
     *
     * @param id_
     * @return
     */
    public Map<String, List<InventoryDetail>> getDataByOrderNo(String id_) {
        //获得二级列表
        Map<String, List<InventoryDetail>> inventoryList = new HashMap<String, List<InventoryDetail>>();
        List<InventoryDetail> inventoryDetailList = DaoManager.
                getDaoSession(MyApplication.APP_CONTEXT).getInventoryDetailDao().queryBuilder().
                where(InventoryDetailDao.Properties.Id_.eq(id_)).
                where(InventoryDetailDao.Properties.Status.notEq(Constants.STATUS_ONE))
                .where(InventoryDetailDao.Properties.IsAdd.eq(false)).list();
        inventoryList.put(MyApplication.APP_CONTEXT.getResources().getString(R.string.inventory_normal),
                inventoryDetailList);
        List<InventoryDetail> inventoryDetailList1 = DaoManager.
                getDaoSession(MyApplication.APP_CONTEXT).getInventoryDetailDao().queryBuilder().
                where(InventoryDetailDao.Properties.Id_.eq(id_)).
                where(InventoryDetailDao.Properties.Status.notEq(Constants.STATUS_ONE))
                .where(InventoryDetailDao.Properties.IsAdd.eq(true)).list();
        inventoryList.put(MyApplication.APP_CONTEXT.getResources().getString(R.string.inventory_more),
                inventoryDetailList1);
        DaoManager.getDaoSession(MyApplication.APP_CONTEXT).clear();
        if (inventoryList != null) {
            return inventoryList;
        }
        return null;
    }

    public Inventory getInventoryByOrderNo(String orderNo) {
        Inventory inventory = DaoManager.
                getDaoSession(MyApplication.APP_CONTEXT).getInventoryDao().queryBuilder().distinct()
                .orderDesc(InventoryDao.Properties.Id_).
                        where(InventoryDao.Properties.Id_.eq(orderNo)).
                        where(InventoryDao.Properties.Status.notEq(Constants.STATUS_ONE)).list().get(0);
        DaoManager.getDaoSession(MyApplication.APP_CONTEXT).clear();
        if (inventory != null) {
            return inventory;
        }
        return null;
    }
}
